package controllers

import (
	"net/http"
	// "strconv"

	"github.com/gin-gonic/gin"
	"github.com/diogoxiang/smock/database"
	"github.com/diogoxiang/smock/models"
)

// GetAllRequestLogs 获取所有请求日志
func GetAllRequestLogs(c *gin.Context) {
	var logs []models.RequestLog
	result := database.DB.Order("created_at DESC").Find(&logs)
	if result.Error != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()})
		return
	}
	c.JSON(http.StatusOK, logs)
}

// GetRequestLog 获取单个请求日志详情
func GetRequestLog(c *gin.Context) {
	id := c.Param("id")
	var log models.RequestLog
	result := database.DB.First(&log, id)
	if result.Error != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Request log not found"})
		return
	}
	c.JSON(http.StatusOK, log)
}

// DeleteRequestLog 删除单个请求日志
func DeleteRequestLog(c *gin.Context) {
	id := c.Param("id")
	var log models.RequestLog
	if err := database.DB.First(&log, id).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Request log not found"})
		return
	}
	
	database.DB.Delete(&log)
	c.JSON(http.StatusOK, gin.H{"message": "Request log deleted successfully"})
}

// ClearRequestLogs 清空所有请求日志
func ClearRequestLogs(c *gin.Context) {
	database.DB.Exec("DELETE FROM request_logs")
	c.JSON(http.StatusOK, gin.H{"message": "All request logs cleared successfully"})
}